package com.ruoyi.lvlian.domain;

import com.alibaba.excel.annotation.ExcelProperty;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.ruoyi.common.core.utils.DateUtils;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.ToString;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;

/**
 * 合同信息对象 lv_contract_info
 *
 * @author liyuxi
 * @date 2024-07-22
 */
@Data
@ToString
public class ContractInfo {
    private static final long serialVersionUID = 1L;

    private String id;
    @ExcelProperty("合同编号")
    @ApiModelProperty(value = "合同编号")
    private String contractNum;

    @ExcelProperty("合同类型")
    @ApiModelProperty(value = "合同类型 1=,采=购合同,2=销售合同")
    private String contractType;

    @ExcelProperty("合同名称")
    @ApiModelProperty(value = "合同名称")
    private String contractName;

    @ExcelProperty("购买方税号")
    @ApiModelProperty(value = "购买方税号")
    private String buyerCode;

    @ExcelProperty("购买方联系人")
    @ApiModelProperty(value = "购买方联系人")
    private String buyerName;

    @ExcelProperty("购买方联系电话")
    @ApiModelProperty(value = "购买方联系电话")
    private String buyerPhone;

    @ExcelProperty("购买方名称")
    @ApiModelProperty(value = "购买方名称")
    private String buyerCompanyName;


    private String contractStatus;

    @ExcelProperty("销售方税号")
    @ApiModelProperty(value = "销售方税号")
    private String sellerCode;

    @ExcelProperty("销售方联系人")
    @ApiModelProperty(value = "销售方联系人")
    private String sellerName;

    @ExcelProperty("销售方联系电话")
    @ApiModelProperty(value = "销售方联系电话")
    private String sellerPhone;

    @ExcelProperty("销售方名称")
    @ApiModelProperty(value = "销售方名称")
    private String sellerCompanyName;


    @ExcelProperty("商品类型")
    @ApiModelProperty(value = "商品类型")
    private String goodsName;

    @ExcelProperty("签订日期")
    @JsonFormat(pattern = "yyyy-MM-dd")
    @ApiModelProperty(value = "签订日期"  ,
            example = "2025-05-01",  // 设置示例时间格式
            notes = "格式：yyyy-MM-dd"  // 说明时间格式
    )
    private String signDate;

    @ExcelProperty("合同生效日期")
    @JsonFormat(pattern = "yyyy-MM-dd")
    @ApiModelProperty(value = "合同生效日期" ,
            example = "2025-05-01",  // 设置示例时间格式
            notes = "格式：yyyy-MM-dd"  // 说明时间格式
    )
    private String startDate;

    @ExcelProperty("合同失效日期")
    @JsonFormat(pattern = "yyyy-MM-dd")
    @ApiModelProperty(value = "合同失效日期"  ,
            example = "2025-05-01",  // 设置示例时间格式
            notes = "格式：yyyy-MM-dd"  // 说明时间格式
    )
    private String endDate;


    @ApiModelProperty(value = "合同金额")
    private Double amount;

    @ApiModelProperty(value = "合同图像pdf")
    private String contractImg;

    @ExcelProperty("合同总额")
    @ApiModelProperty(value = "合同总额")
    private String contractAmount;

    @ApiModelProperty(value = "录入人")
    private String createUser;

    @JsonFormat(pattern = "yyyy-MM-dd")
    @ApiModelProperty(value = "登记时间 yyyy-MM-dd"  ,
            example = "2025-05-01",  // 设置示例时间格式
            notes = "格式：yyyy-MM-dd"  // 说明时间格式
    )
    private String createDate;

    @ExcelProperty("所属公司编码")
    private String orgCode;

    @ApiModelProperty(value = "备注")
    private String remark;


    /** 付款方式 */
    @ApiModelProperty(value = "付款方式")
    private String payType;

    /** 签章状态 */
    @ApiModelProperty(value ="签章状态")
    private String signatureStatus;

    /** 开户银行 */
    @ApiModelProperty(value ="开户银行")
    private String openBank;

    /** 银行账户 */
    @ApiModelProperty(value ="银行账户")
    private String bankAccno;
    @ApiModelProperty(value ="合同链接")
    private String pageUrl;



    public String getSignDate() {
        if(StringUtils.isEmpty( signDate)){
            return null;
        }
        if (StringUtils.isNotEmpty(signDate) && DateUtils.isValidDateTime(signDate)) {
            signDate = signDate.substring(0, 10);
        }
        if (StringUtils.isNotEmpty(signDate)) {
            signDate = DateUtils.unifyDateFormats(signDate);
        }

        return signDate;
    }

    public String getStartDate() {
        if(StringUtils.isEmpty( startDate)){
            return null;
        }
        if (StringUtils.isNotEmpty(startDate) && DateUtils.isValidDateTime(startDate)) {
            startDate = startDate.substring(0, 10);
        }
        if (StringUtils.isNotEmpty(startDate)) {
            startDate = DateUtils.unifyDateFormats(startDate);
        }

        return startDate;
    }

    public String getEndDate() {
        if(StringUtils.isEmpty( endDate)){
            return null;
        }
        if (StringUtils.isNotEmpty(endDate) && DateUtils.isValidDateTime(endDate)) {
            endDate = endDate.substring(0, 10);
        }
        if (StringUtils.isNotEmpty(endDate)) {
            endDate = DateUtils.unifyDateFormats(endDate);
        }

        return endDate;
    }

    public String getBuyerName() {
        return buyerName;
    }

    public void setBuyerName(String buyerName) {
        this.buyerName = buyerName;
    }

    public String getContractImg() {
        if ("https://data.lvlianhengsheng.com/dev-api".equalsIgnoreCase(contractImg) || StringUtils.countMatches(contractImg, "http") > 1) {
            return null;
        }
        if (com.ruoyi.common.core.utils.StringUtils.isNotEmpty(contractImg) && !contractImg.contains("http")) {
            return "https://data.lvlianhengsheng.com/dev-api/file/fileAttachmentInfo/viewPdf?key=" + contractImg;
        }
        return contractImg;
    }

    public String getContractStatus() {
        try {
            Date nowDate = DateUtils.getNowDate();
            Date start = DateUtils.parseDate(startDate);
            Date end = DateUtils.parseDate(endDate);
            if (nowDate.getTime() >= start.getTime() && nowDate.getTime() <= end.getTime()) {
                contractStatus = "有效";
            } else {
                contractStatus = "已过期";
            }
        } catch (Exception e) {
            contractStatus = "已过期";
            return "已过期";
        } finally {
            return contractStatus;
        }
    }

    public void setSellerCompanyName(String sellerCompanyName) {
        this.sellerCompanyName = sellerCompanyName;
    }


}
